import Api, { DO_PAY_LIST, CREATE_ORDER, PAY_ORDER, GET_DEFAULT_ADDR, SET_PREPAYID} from '../../utils/api.js';
import { handPic, getUrl, error} from '../../utils/util.js';

// pages/payment/payment.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        address: false,
        ctype: '',
        list:{},
        total_price:0,
        num :0,
        remark:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
       
        let key,
            _this = this;
        if (Api.checkLogin(function () {
            _this.onLoad(options);
        })) {
            key = Api.getKey();
            this.setData({
                key: key
            })
        }
        const ctype = options.type;

        this.setData({
            ctype: ctype
        })
        Api.fetch(DO_PAY_LIST,{key:key,ctype:ctype}, resp => {
            resp.list.map((v,k) => {
                v.goods_img = handPic(v.goods_img,4);
            })
            
            this.setData({
                list:resp.list,
                total_price:resp.total_price,
                num :resp.num
            })
        })

        Api.fetch(GET_DEFAULT_ADDR, { key: key }, resp => {
            if (resp) {
                this.setData({
                    address: resp
                })
            }

        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //  收货地址
    choose_address: function () {
        const _this = this;
        wx.chooseAddress({
            success: function (res) {
                let address = {
                    name: res.userName,
                    mobile: res.telNumber,
                    prov: res.provinceName,
                    city: res.cityName,
                    county: res.countyName,
                    detail: res.detailInfo
                }
                _this.setData({
                    address: address
                });
            }
        })
    },
    bindremark :function(event){
        const {value} = event.detail;
        
        this.setData({
            remark:value
        })
    },

    //创建订单
    create:function(){

        const {key,address,ctype,remark} = this.data;

        if(!address){
            error("请选择收货地址!");
            return;
        }

        wx.showLoading({
            title: '订单创建中',
            mask: true
        })

        
        Api.fetch(CREATE_ORDER,{
            key:key,
            name:address.name,
            mobile:address.mobile,
            prov:address.prov,
            city:address.city,
            county:address.county,
            detail:address.detail,
            ctype: ctype,
            remark:remark
        },resp => {
            Api.fetch(PAY_ORDER,{key:key,order_sn:resp}, config =>{
                console.log(config);
                wx.requestPayment({
                    'timeStamp': config.timeStamp,
                    'nonceStr': config.nonceStr,
                    'package': config.package,
                    'signType': 'MD5',
                    'paySign': config.paySign,
                    'success': function (res) {
                        Api.fetch(SET_PREPAYID,{key:key,order_sn:resp,prepay_id:config.prepay_id},s => {
                            wx.hideLoading();
                            wx.redirectTo({
                                url: '/pages/success/success',
                            })
                        })
                       
                    }, 
                    'fail': function (res) {
                        wx.hideLoading();
                        wx.showToast({
                            title: '支付取消',
                        })
                    }
                })
            })
        })
       
    }
})